package com.zch.house.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zch.house.entity.Logistics;
import com.zch.house.service.LogisticsService;
import com.zch.house.service.impl.LogisticsServiceImpl;
import com.zch.house.util.DateFormat;

/**
 * Servlet implementation class LogisticsServlet
 */
@WebServlet("/LogisticsServlet2")
public class LogisticsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       LogisticsService ls = new LogisticsServiceImpl();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LogisticsServlet() {
        super();
    }
    /**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String method = request.getParameter("method");
		if("add".equals(method)){
			add(request,response);
		}else if("delete".equals(method)){
			delete(request,response);
		}else if("update".equals(method)){
			update(request,response);
		}else if("select".equals(method)){
			select(request,response);
		}else if("glbi".equals(method)){
			getLogisticsById(request,response);
		}
	}

	private void add(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String lo_name = request.getParameter("name");
		String lo_id_card = request.getParameter("idCard");
		String lo_phone = request.getParameter("phone");
		 String lo_sex = request.getParameter("sex");
		String lo_post = request.getParameter("post");
		String createTimeStr = request.getParameter("createTime");
		Date lo_create_time = DateFormat.to_Date2(createTimeStr);
		String salStr = request.getParameter("sal");
		double lo_sal = Double.parseDouble(salStr);
		String lo_native_place = request.getParameter("nativePlace");
		String statusStr = request.getParameter("status");
		int lo_status = Integer.parseInt(statusStr);
		Logistics logistics = new Logistics(lo_name, lo_id_card, lo_phone, lo_sex, lo_post,
				lo_create_time, lo_sal, lo_native_place, lo_status);
		boolean flag = ls.addLogistics(logistics);
		PrintWriter out = response.getWriter();
		if (flag) {
			// 添加成功,将添加成功的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('添加成功！');window.location.href='LogisticsServlet2?method=select'</script>");
		} else {
			// 添加失败,将添加失败的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('添加失败！');window.location.href='LogisticsServlet2?method=select'</script>");
		}
	}

	private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String idStr = request.getParameter("id");
		int id = Integer.parseInt(idStr);
		boolean flag = ls.deleteLogistics(id);
		// 实例化一个out对象
		PrintWriter out = response.getWriter();
		if (flag) {
			// 删除成功,将删除成功的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('删除成功！');window.location.href='LogisticsServlet2?method=select'</script>");
		} else {
			// 删除失败,将删除失败的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('删除失败！');window.location.href='LogisticsServlet2?method=select'</script>");
		}
	}

	private void update(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String idStr = request.getParameter("id");
		int id = Integer.parseInt(idStr);
		String lo_name = request.getParameter("name");
		String lo_id_card = request.getParameter("idCard");
		String lo_phone = request.getParameter("phone");
		 String lo_sex = request.getParameter("sex");
		String lo_post = request.getParameter("post");
		String createTimeStr = request.getParameter("createTime");
		Date lo_create_time = DateFormat.to_Date2(createTimeStr);
		String salStr = request.getParameter("sal");
		double lo_sal = Double.parseDouble(salStr);
		String lo_native_place = request.getParameter("nativePlace");
		String statusStr = request.getParameter("status");
		int lo_status = Integer.parseInt(statusStr);
		Logistics logistics = new Logistics(id, lo_name, lo_id_card, lo_phone, 
				lo_sex, lo_post, lo_create_time, lo_sal, lo_native_place, lo_status);
		boolean flag = ls.updateLogistics(logistics);
		PrintWriter out = response.getWriter();
		if (flag) {
			// 修改成功,将修改成功的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('修改成功！');window.location.href='LogisticsServlet2?method=select'</script>");
		} else {
			// 修改失败,将修改失败的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('修改失败！');window.location.href='LogisticsServlet2?method=select'</script>");
		}
	}

	private void select(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String name = request.getParameter("name"); 
		String phone = request.getParameter("phone"); 
		String sex = request.getParameter("sex");
		String pageIndexStr = request.getParameter("pageIndex");
		if(pageIndexStr==null||pageIndexStr==""){
			pageIndexStr = "1";
		}
		int pageIndex = Integer.parseInt(pageIndexStr);
		//获得列表
		List<Logistics> list = ls.getAllLogisticsByPage(name, phone, sex, pageIndex);
		//获得总页码
		int totalPages = ls.getTotalCount(name, phone, sex);
		//将参数输入作用域
		request.setAttribute("name", name);
		request.setAttribute("phone", phone);
		request.setAttribute("sex", sex);
		request.setAttribute("pageIndex", pageIndex);
		request.setAttribute("list", list);
		request.setAttribute("totalPages", totalPages);
		request.getRequestDispatcher("view/logistics/list.jsp").forward(request, response);
		
	}

	private void getLogisticsById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String idStr = request.getParameter("id");
		int id = Integer.parseInt(idStr);
		Logistics logistics = ls.getLogisticsById(id);
		request.setAttribute("logistics", logistics);
		request.getRequestDispatcher("view/logistics/edit.jsp").forward(request, response);		
	}

}
